

#include <pic.h>
#include <htc.h> 
#include <stdio.h>
#include <stdlib.h>
#include "Init.h"
#include "SysClock.h"
#include "my_macros.h"
#include "App.h"
#include "outputs.h"
#include "hardware.h"

#define   APP_START		0
#define   APP_MAIN		1
#define   APP_FINISH		2


static unsigned char app_state = APP_START;

typedef struct
{
	unsigned int time;
	unsigned int outputs;
}time_point_t;


#if defined(C28_FIRST_BOARD)

const time_point_t table [] =
{
	{0,   0b0000000000000001},
	{10,  0b0000000000000011},
	{100, 0b0000000000000101},
	{190, 0b0000000000001001},
	{280, 0b0000000000010001},
	{370, 0b0000000000100001},
	{460, 0b0000000001000001},
	{550, 0b0000000010000001},
	{640, 0b0000000100000001},
	{730, 0b0000001000000001},
	{820, 0b0000010000000001},
	{910, 0b0000100000000001},
	{1000,0b0001000000000001},
	{1090,0b0010000000000001},
	{1180,0b0000000000000001},
	{2370,0b0000000000000000},
};

#else	 /* second board */

const time_point_t table [] =
{
	{0,    0b0000000000000000},
	{1180, 0b0000000000000001},
	{1270, 0b0000000000000010},
	{1360, 0b0000000000000100},
	{1450, 0b0000000000001000},
	{1540, 0b0000000000010000},
	{1630, 0b0000000000100000},
	{1720, 0b0000000001000000},
	{1810, 0b0000000010000000},
	{1900, 0b0000000100000000},
	{1990, 0b0000001000000000},
	{2080, 0b0000010000000000},
	{2170, 0b0000100000000000},
	{2260, 0b0001000000000000},
	{2350, 0b0010000000000000},
	{2370, 0b0000000000000000},
};

#endif


unsigned int tcnt;
unsigned char step;

void App ( void )
{

	if ( app_state != APP_FINISH && Time_500ms )
	{
		LED ^= 1;
	}


	if ( (!TEST_BTN && Time_100ms) || (TEST_BTN && Time_1000ms) )
	{
		tcnt++;
	}

	switch( app_state )
	{
		case APP_START:
			app_state = APP_MAIN;
			tcnt = 0;
			step = 0;
			
		case APP_MAIN:
			if ( table[step].time == tcnt )
			{
				Out_Status = table[step].outputs;
				if ( ++step >= (sizeof(table)/sizeof(time_point_t)) )
				{ 
					app_state = APP_FINISH;
				}
			}
			break;
			
		case APP_FINISH:
			LED = 0;
			break;
	}  
}
